<!--
 * @Author: zhangdong 354266323@qq.com
 * @Date: 2022-06-07 16:56:53
 * @LastEditors: zhangdong 354266323@qq.com
 * @LastEditTime: 2022-06-17 09:27:47
 * @FilePath: \logistics-sunshine-unattended-app\App.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script>
import { mapMutations } from 'vuex'
export default {
  onLaunch: function () {
    this.setPermissionsInform()
    let userInfo = uni.getStorageSync('userInfo')
    if (userInfo.expireDocument) {
      const expireDocument = JSON.parse(userInfo.expireDocument)
      console.log(expireDocument)
      const str = expireDocument.join('、')
      uni.showModal({
        title: '提示',
        content: `您的${str}已过期`,
        showCancel: false,
        success: ({ confirm, cancel }) => {}
      })
    }
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        // 如果有新版本提醒并进行强制升级
        uni.showModal({
          content: '新版本已经准备好，是否重启应用？',
          showCancel: false,
          confirmText: '确定',
          success: (res) => {
            if (res.confirm) {
              updateManager.onUpdateReady((res) => {
                // 新版本下载完成的回调
                updateManager.applyUpdate() // 强制当前小程序应用上新版本并重启
              })

              updateManager.onUpdateFailed((res) => {
                // 新版本下载失败的回调
                // 新版本下载失败，提示用户删除后通过冷启动重新打开
                uni.showModal({
                  content: '下载失败，请删除当前小程序后重新打开',
                  showCancel: false,
                  confirmText: '知道了'
                })
              })
            }
          }
        })
      }
    })
  },
  methods: {
    ...mapMutations(['login', 'logout']),
    //上报访问次数
    async addVistLog() {
      let mobile = { mobile: uni.getStorageSync('username') }
      await this.$u.post('/freight-business/driverVisitLog/addLog', mobile)
    },
    /**
     * 1，设置手机通知权限
     */
    setPermissionsInform() {
      // #ifdef APP-PLUS
      if (plus.os.name == 'Android') {
        // 判断是Android
        var main = plus.android.runtimeMainActivity()
        var pkName = main.getPackageName()
        var uid = main.getApplicationInfo().plusGetAttribute('uid')
        var NotificationManagerCompat = plus.android.importClass(
          'android.support.v4.app.NotificationManagerCompat'
        )
        //android.support.v4升级为androidx
        if (NotificationManagerCompat == null) {
          NotificationManagerCompat = plus.android.importClass(
            'androidx.core.app.NotificationManagerCompat'
          )
        }
        var areNotificationsEnabled =
          NotificationManagerCompat.from(main).areNotificationsEnabled()
        // 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
        if (!areNotificationsEnabled) {
          uni.showModal({
            title: '通知权限开启提醒',
            content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
            showCancel: false,
            confirmText: '去设置',
            success: function (res) {
              if (res.confirm) {
                var Intent = plus.android.importClass('android.content.Intent')
                var Build = plus.android.importClass('android.os.Build')
                //android 8.0引导
                if (Build.VERSION.SDK_INT >= 26) {
                  var intent = new Intent(
                    'android.settings.APP_NOTIFICATION_SETTINGS'
                  )
                  intent.putExtra('android.provider.extra.APP_PACKAGE', pkName)
                } else if (Build.VERSION.SDK_INT >= 21) {
                  //android 5.0-7.0
                  var intent = new Intent(
                    'android.settings.APP_NOTIFICATION_SETTINGS'
                  )
                  intent.putExtra('app_package', pkName)
                  intent.putExtra('app_uid', uid)
                } else {
                  //(<21)其他--跳转到该应用管理的详情页
                  intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                  var uri = Uri.fromParts(
                    'package',
                    mainActivity.getPackageName(),
                    null
                  )
                  intent.setData(uri)
                }
                // 跳转到该应用的系统通知设置页
                main.startActivity(intent)
              }
            }
          })
        }
      }
      // #endif
    }
  },
  onShow: function (e) {
    let userInfo = uni.getStorageSync('userInfo')
    if (userInfo) {
      // 已经登录
      this.addVistLog()
    }
  },
  onHide: function () {
    console.log('App Hide')
  }
}
</script>

<style lang="scss">
@import 'uview-ui/index.scss';
/*每个页面公共css */
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import 'uview-ui/index.scss';
// page {
//   background-color: #ffffff;
// }

/*
		全局公共样式和字体图标
	*/
@font-face {
  font-family: yticon;
  font-weight: normal;
  font-style: normal;
  src: url('https://at.alicdn.com/t/font_1078604_w4kpxh0rafi.ttf')
    format('truetype');
}

.yticon {
  font-family: 'yticon' !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-fork:before {
  content: '\e61b';
}

.line-limit {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap; //文本不换行，这样超出一行的部分被截取，显示...
}
</style>
